/**
 * 
 */
package org.wolf.common.util;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

/**
 * @author 何小锋
 * 
 */
public class NetUtil {

	/**
	 * 得到所有的IP地址
	 * 
	 * @return
	 * @throws SocketException
	 * @throws UnknownHostException
	 */
	public static Set<String> getAllAddress() throws SocketException,
			UnknownHostException {
		Set<String> result = new HashSet<String>();
		Enumeration<NetworkInterface> netInterfaces = NetworkInterface
				.getNetworkInterfaces();

		InetAddress ip;

		NetworkInterface ni;
		Enumeration<InetAddress> ia;
		while (netInterfaces.hasMoreElements()) {
			ni = (NetworkInterface) netInterfaces.nextElement();
			ia = ni.getInetAddresses();
			while (ia.hasMoreElements()) {
				ip = (InetAddress) ia.nextElement();
				if (ip != null && !ip.isLoopbackAddress()
						&& ip instanceof Inet4Address) {
					result.add(ip.getHostAddress());
					result.add(ip.getHostName());
				}
			}

		}
		return result;
	}

}
